
package com.platform.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 对象操作工具类, 继承org.apache.commons.lang.ObjectUtils类
 *
 * @author 肖亮
 */
public class ObjectUtils extends org.apache.commons.lang.ObjectUtils {

	private static Log logger = LogFactory.getLog(ObjectUtils.class);

	/**
	 * 注解到对象复制，只复制能匹配上的方法。
	 *
	 * @param annotation
	 *            annotation
	 * @param object
	 *            object
	 */
	public static void annotationToObject(Object annotation, Object object) {
		if (annotation != null) {
			Class<?> annotationClass = annotation.getClass();
			Class<?> objectClass = object.getClass();
			for (Method m : objectClass.getMethods()) {
				if (StringUtils.startsWith(m.getName(), "set")) {
					try {
						String s = StringUtils.uncapitalize(StringUtils.substring(m.getName(), 3));
						Object obj = annotationClass.getMethod(s).invoke(annotation);
						if (obj != null && !"".equals(obj.toString())) {
							if (object == null) {
								object = objectClass.newInstance();
							}
							m.invoke(object, obj);
						}
					} catch (Exception e) {
						// 忽略所有设置失败方法
					}
				}
			}
		}
	}

	/**
	 * 序列化对象
	 *
	 * @param object
	 *            object
	 * @return byte[]
	 */
	public static byte[] serialize(Object object) {
		try {
			if (object != null) {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ObjectOutputStream oos = new ObjectOutputStream(baos);
				oos.writeObject(object);
				return baos.toByteArray();
			}
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}
		return null;
	}

	/**
	 * 反序列化对象
	 *
	 * @param bytes
	 *            bytes
	 * @return Object
	 */
	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais;
		try {
			if (bytes != null && bytes.length > 0) {
				bais = new ByteArrayInputStream(bytes);
				ObjectInputStream ois=null;
				try {
					ois = new ObjectInputStream(bais);
					return ois.readObject();
				} catch (Exception e) {
				}
				
			}
		} catch (Exception e) {
			//logger.error(ExceptionUtils.getStackTrace(e));
		}
		return null;
	}
}
